from odoo import models, api


class SaleOrder(models.Model):
    _inherit = 'sale.order'

    @api.constrains('status')
    def constrains_status_one(self):
        """
        销售订单取消，自动解锁备料
        """
        for sale in self:
            if sale.status == 'cancelled' or sale.state == 'cancel':
                product_lock_ids = self.env['s.product.lock'].search([('sale_id', '=', sale.id)])
                product_lock_ids.action_unlock()
